using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Policy;
using System.Security.Permissions;

[assembly: CLSCompliant(true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution = true)]
namespace CodeColorerDotNet
{
    public partial class MainForm : Form
    {
        String fileName = "<NoName>";
        List<String> schemes;
        int currentScheme;
        public MainForm()
        {
            InitializeComponent();
            codeTextBox.SelectAll();
            codeTextBox.SelectionAlignment = HorizontalAlignment.Center;
            codeTextBox.SelectionColor = Color.DarkGray;
            codeTextBox.Select(0, 0);
            schemes = new List<String>();
            String path = Path.GetDirectoryName(Application.ExecutablePath);
            DirectoryInfo di = new DirectoryInfo(path + "\\Schemes\\");
            // Create an array representing the files in the current directory.
            FileInfo[] fi = di.GetFiles();

            for (int i = 0; i < fi.Length; i++)
            {
                schemes.Add(fi[i].FullName);
                ColorScheme tempScheme = new ColorScheme(fi[i].FullName);
                ToolStripItem item = new ToolStripMenuItem();
                item.Text = tempScheme.Name;
                item.Tag = i;
                toolDropDownSchemes.DropDownItems.Add(item);
                String addin = "";
                if (openFileDialog.Filter.Length != 0)
                    addin = "|";
                openFileDialog.Filter += addin + tempScheme.Name + "|" + tempScheme.Extensions;
            }
            String adding = "";
            if (openFileDialog.Filter.Length != 0)
                adding = "|";
            openFileDialog.Filter += adding + "Any File|*.*";
            toolDropDownSchemes.Text = "&Scheme - " + toolDropDownSchemes.DropDownItems[currentScheme].Text;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //find current scheme
                fileName = openFileDialog.FileName;
                Text = fileName + " - CodeColorer.NET";
                if (openFileDialog.FilterIndex - 1 < schemes.Count)
                    currentScheme = openFileDialog.FilterIndex - 1;
                toolDropDownSchemes.Text = "&Scheme - " + toolDropDownSchemes.DropDownItems[currentScheme].Text;
                //    
                ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
                nowScheme.ConvertRtf(fileName, codeTextBox);
                toolDropDownSave.Enabled = true;
                toolDropDownClipboard.Enabled = true;
                toolDropDownSchemes.Enabled = true;
                codeTextBox.Enabled = true;
            }
        }

        private void toolDropDownSchemes_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            currentScheme = (int)e.ClickedItem.Tag;
            toolDropDownSchemes.Text = "&Scheme - " + e.ClickedItem.Text;
            if (fileName != "<NoName>")
            {
                ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
                nowScheme.ConvertRtf(fileName, codeTextBox);
            }
        }

        private void toolButtonRtf_Click(object sender, EventArgs e)
        {
            if (saveRtfDialog.ShowDialog() == DialogResult.OK)
            {
                codeTextBox.SaveFile(saveRtfDialog.FileName);
                System.Diagnostics.Process.Start(saveRtfDialog.FileName);
            }
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            AboutForm about = new AboutForm();
            about.ShowDialog();
        }

        private void simpleHTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveHtmlDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveHtmlDialog.FileName);
                ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
                sw.Write(nowScheme.ConvertHtmlSimple(fileName, codeTextBox));
                sw.Close();
                System.Diagnostics.Process.Start(saveHtmlDialog.FileName);
            }
        }

        private void hTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveHtmlDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveHtmlDialog.FileName);
                ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
                sw.Write(nowScheme.ConvertHtml(fileName, codeTextBox));
                sw.Close();
                System.Diagnostics.Process.Start(saveHtmlDialog.FileName);
            }
        }

        private void bBCodesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveBbDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveBbDialog.FileName);
                ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
                sw.Write(nowScheme.ConvertBbCodes(codeTextBox));
                sw.Close();
                System.Diagnostics.Process.Start(saveBbDialog.FileName);
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(codeTextBox.Rtf, true);
        }

        private void textToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(codeTextBox.Text, true);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
            Clipboard.SetDataObject(nowScheme.ConvertHtml(fileName, codeTextBox), true);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
            Clipboard.SetDataObject(nowScheme.ConvertHtmlSimple(fileName, codeTextBox), true);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            ColorScheme nowScheme = new ColorScheme(schemes[currentScheme]);
            Clipboard.SetDataObject(nowScheme.ConvertBbCodes(codeTextBox), true);
        }

        private void textToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (saveBbDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveBbDialog.FileName);
                sw.Write(codeTextBox.Text);
                sw.Close();
                System.Diagnostics.Process.Start(saveBbDialog.FileName);
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //load settings
            CodeColorerDotNet.Properties.Settings sets = CodeColorerDotNet.Properties.Settings.Default;
            Left = sets.Left;
            Top = sets.Top;
            Width = sets.Width;
            Height = sets.Height;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //save settings
            CodeColorerDotNet.Properties.Settings sets = CodeColorerDotNet.Properties.Settings.Default;
            sets.Left = Left;
            sets.Top = Top;
            sets.Width = Width;
            sets.Height = Height;
            sets.Save();
        }

    }
}